package com.ledim.ledimview;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.ledim.activity.EpisodesActivity;
import com.ledim.adapter.TVEpisodesAdapter;
import com.ledim.bean.LedimEpisodeBean;
import com.ledim.bean.SubmediaEpisodesResponse;
import com.ledim.service.LedimApiServiceUtils;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.ToastUtil;
import com.letv.android.young.client.R;
import java.util.ArrayList;

public class ViewPagerEpisodeListView extends LinearLayout {

    private Context mContext;
    private GridView mTVGridView;
    private int mEpisodes;
    private int mTabPosition;
    private boolean mAddPlay;
    public TVEpisodesAdapter mFigureAdapter;
    private String mMediaId;
    private String mSubMediaId;
    public boolean mIsReload;//控制每个viewpager的页面是否都要加载
    private ArrayList<String> mEpisodesList = new ArrayList<String>();
    public ArrayList<LedimEpisodeBean> mAllTVEpisodesList = new ArrayList<LedimEpisodeBean>();
    private LinearLayout mTabView;

    public ViewPagerEpisodeListView(Context context) {
        super(context);
        mContext = context;
    }

    public ViewPagerEpisodeListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public ViewPagerEpisodeListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    private EpisodesActivity.EpisodesInterface mEpisodesInterface;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    //绑定数据
    public void bindData(boolean mAddPlay, String media, String submedia, int mEpisodes,
                         int mTabPosition, boolean isReload,
                         EpisodesActivity.EpisodesInterface mEpisodesInterface,
                         LinearLayout tabView) {
        this.mIsReload = isReload;
        this.mAddPlay = mAddPlay;
        this.mMediaId = media;
        this.mSubMediaId = submedia;
        this.mEpisodes = mEpisodes;
        this.mTabPosition = mTabPosition;
        this.mEpisodesInterface = mEpisodesInterface;
        this.mTabView = tabView;
        init();

    }

    /**
     * 初始化界面
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void init() {
        mTVGridView = (GridView) findViewById(R.id.gridview_tv);
        if (mAddPlay) {
            mTVGridView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        } else {
            mTVGridView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
        if (mTabPosition == 0) {
            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 50, 0, 2, 1, mediaListObserver);
        } else {
            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 50, 0, 0,
                    mTabPosition + 1, mediaListObserver);
        }

    }

    private LedimHttpObserver mediaListObserver =
            new LedimHttpObserver<SubmediaEpisodesResponse>() {
                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                }

                @Override
                public void onNext(SubmediaEpisodesResponse response) {
                    super.onNext(response);
                    if (response != null) {
                        if (response.success && response.data.size() > 0) {
                            response.data = LedimApiServiceUtils.getEpisodeDataLists(response.data);
                            mAllTVEpisodesList.clear();
                            mAllTVEpisodesList.addAll(response.data);
                            mTabView.setVisibility(View.VISIBLE);
                            showEpisodesGridView(mTabPosition, mEpisodes, response.data);
                        } else {
                            mTabView.setVisibility(View.GONE);
                            ToastUtil.toast("你所请求的资源沉到海底去了");
                        }
                    }
                }
            };

    /**
     * 显示数字剧集
     */
    private void showEpisodesGridView(int i, int mEpisodes, ArrayList<LedimEpisodeBean> episodes) {

        mTVGridView.setVisibility(View.VISIBLE);
        mEpisodesList.clear();
        if (50 * (i + 1) > mEpisodes) {
            for (int j = 50 * i + 1; j <= mEpisodes; j++) {
                mEpisodesList.add(j + "");
            }
        } else {
            for (int j = 50 * i + 1; j <= 50 * (i + 1); j++) {
                mEpisodesList.add(j + "");
            }
        }
        if (mFigureAdapter == null) {
            mFigureAdapter =
                    new TVEpisodesAdapter(mContext, mEpisodesList, episodes, mAddPlay, mEpisodesInterface);
            mTVGridView.setAdapter(mFigureAdapter);
        } else {
            mFigureAdapter.pList = mEpisodesList;
            mFigureAdapter.pEpisodes = episodes;
            mFigureAdapter.mAddPlay = mAddPlay;
            mFigureAdapter.notifyDataSetChanged();
        }

        for (int j = 0; j < episodes.size(); j++) {
            if (episodes.get(j).id == Integer.valueOf(mSubMediaId)) {
                mFigureAdapter.setSelectItem(j);//位置
                //gridview显示指定位置
                mFigureAdapter.notifyDataSetChanged();
            }
        }
    }
}
